package com.lan.workflow.controller;


import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.lan.workflow.annotation.Log;
import com.lan.workflow.common.ResponseCodeEnum;
import com.lan.workflow.service.ILeaveApplyService;
import com.lan.workflow.entity.LeaveApply;
import io.swagger.annotations.Api;
import org.springframework.web.bind.annotation.*;
import org.springframework.beans.factory.annotation.Autowired;

import java.util.*;
import org.springframework.web.bind.annotation.RequestParam;

/**
 * @author lanwp
 * @since 2020-08-13
 */
@Api(value = "请假流程" , tags = "请假流程")
@RestController
@RequestMapping("/leaveApply")
public class LeaveApplyController {

    @Autowired
    private ILeaveApplyService leaveApplyService;

    @Log
    @PostMapping(value = "/page", produces = {"application/json;charset=utf-8;"})
    public Map<String,Object> todoPage(@RequestParam Integer size, Integer current, @RequestBody Map<String,Object>  requestMap){
        Map<String, Object> result = new HashMap<>();
        IPage<LeaveApply> page = new Page<>();
        page.setSize(size);
        page.setCurrent(current);
        boolean todoList = requestMap.get("todoList") == null ? false : Boolean.valueOf(requestMap.get("todoList").toString());
        if(todoList){
            //查询待办，加载用户数据
            requestMap.put("assignee", "000002");
            requestMap.put("ruleList", Arrays.asList("bmjl","rs"));
        }
        page = leaveApplyService.page(page, requestMap);
        result.put("code", ResponseCodeEnum.SUCCESS.getKey());
        result.put("data", page);
        return result;
    }

    @PostMapping(value = "/saveOrUpdate", produces = {"application/json;charset=utf-8;"})
    public Map<String,Object> saveOrUpdate(@RequestBody LeaveApply leaveApply){
        Map<String, Object> result = new HashMap<>();
        String userId = "000001";
        leaveApply.setUserId(userId);
        leaveApplyService.saveOrUpdate(leaveApply);

        result.put("code", ResponseCodeEnum.SUCCESS.getKey());
        result.put("msg", ResponseCodeEnum.SUCCESS.getValue());
        return result;
    }

    @PostMapping(value = "/startWorkflow", produces = {"application/json;charset=utf-8;"})
    public Map<String,Object> startWorkflow(@RequestBody LeaveApply leaveApply){
        Map<String, Object> result = new HashMap<>();
        String userId = "000001";
        Map<String, Object> variables = new HashMap<String, Object>();
        variables.put("applyuserid", userId);
        leaveApplyService.startWorkflow(leaveApply, userId, variables);
        result.put("code", ResponseCodeEnum.SUCCESS.getKey());
        result.put("msg", ResponseCodeEnum.SUCCESS.getValue());
        return result;
    }
}

